#!/usr/bin/python 
'''
Program is used to parse and configure an individual order or 
collate the individual orders to create an Action Order.  
'''

import argparse
import logging
import xlrd
import sys,os
import ActionItems
from PriceList import get_pricelist_sheet,ItemsTable



# - global variables
# 
tables = []
# log to temporary directory

def setupLogger(logfile):
    temporaryDirectory = os.getenv("TEMP", os.getcwd()) 
    separator = os.sep
    return separator.join([temporaryDirectory, logfile])

logging.basicConfig(filename=setupLogger('ActionMeat.log'))


def parserconfig():
    parser = argparse.ArgumentParser(description="Process Action Meat Spreadsheets", 
                                     epilog="David Duncan <davdunc@gmail.com>\nhttp://http://code.google.com/p/coop-spreadsheet-manager/"
                                     ) 
    parser.add_argument("-a", "--action", metavar="File", help="Import Action Order File.")
    parser.add_argument('-c', '--combine',  nargs='+', metavar="File", help="Combine Order Files")
    parser.add_argument('-d', '--directory', metavar="Directory", default=os.getcwd(), help="Directory to locate or situate files")
    logging.debug("parserconfig runs.  Parser created with args.")
    return parser  
# -

class arguments(object): 
    '''holder for the system arguements'''
    pass

class OrderTable(object): 
    pass
        
def create_MemberOrderSheet(actionPriceList): 
    try:
        orderWorkBook = xlrd.open_workbook(actionPriceList)
        logging.info("Action Price List %s Opened" % actionPriceList)
    except IOError, e: 
        print "Error Importing Workbook for processing."
        print e
        logging.critical(e)
    
    action_OrderSheet = orderWorkBook.sheet_by_name('STANDARD') # Sheet name is hard coded.  FIXME
    logging.debug("FixMe.  Sheetname for actionOrderSheet is hard coded")
    Action_ItemWorkbook = ActionItems.createItemsList(action_OrderSheet) # create an Items list from the Order Sheet.  
# FIXME    ActionItems.createMemberWorkbook(Action_ItemList) # Add items together to create order.
# FIXME    ActionItems.createCoordinatorWorkbook(Action_ItemList)

    #PLACEHOLDER
# MAIN 
def main(args):
    '''Called if prog is called directly'''
    
    if args.action: 
        print "Import Action Order from file: %s" % args.action 
        create_MemberOrderSheet(args)
    elif args.combine:
        print "Aggregate the following documents for Order:"
        for Member_Sheet in args.combine:
            logging.debug("\t%s" % Member_Sheet)
        ActionItems.combine_member_orders(args)    
    else:
        logging.critical("no action specified")

if __name__ == "__main__": 
    parser = parserconfig()
    parser.parse_args(namespace=arguments) 
    main(arguments)



